<template>
    <div class="info">
        <div class="signUp-head">
            <span>用户修改</span>
        </div>
        <hr/>
        <div class="personal">
            <el-form :model="registerForm" ref="registerForm" label-width="80px" class="demo-ruleForm" :rules="rules">
                <el-form-item prop="username" label="用户名">
                    <el-input v-model="registerForm.username" placeholder="username"></el-input>
                </el-form-item>
                <el-form-item prop="password" label="密码">
                    <el-input type="password" v-model="registerForm.password" placeholder="password"></el-input>
                </el-form-item>

            </el-form>
            <div class="btn">
                <div @click="goback(-1)">取消</div>
                <div type="primary" @click="saveMsg">保存</div>
            </div>
        </div>

    </div>
</template>

<script>
    import {mapState} from "vuex"
    import {updateUserMsg} from "../api/index";
    import {mixin} from "../mixins";
    import {rules} from "../assets/data/form";


    export default {
        name: "info",
        mixins:[mixin],
        data(){
            return {
                registerForm:{
                    username:'',
                    password:'',

                },
                rules:{}
            }
        },
        computed:{
            ...mapState("user",{
                userId: state=>state.userId
            }),
        },
        created() {
            this.rules=rules;
        },
        methods:{
            saveMsg(){
                let params=new URLSearchParams();
                let _this=this;
                params.append('userid',this.userId)
                params.append('username',this.registerForm.username);
                params.append('password',this.registerForm.password);
                params.append('photo','photo.jpg');
                updateUserMsg(params).then(res =>{

                    //console.log(res.data.status)
                    if (res.status===1){
                        _this.$store.commit('setUsername',this.registerForm.username)
                        _this.notify('修改成功','success');

                    }else {
                        _this.notify('修改error','error');
                        setTimeout(function () {
                            _this.$router.push({path:'/'});
                        },1000);
                    }

                }).catch(err=>{
                    _this.notify('修改失败','error');
                })

            },
            goback(index){
                this.$router.go(index);
            }

        }

    }
</script>

<style lang="scss" scoped>
@import "../assets/css/info";
</style>